import { reqGoodsInfo, reqAddUpdateCar } from "@/api"

import { GOODS_INFO } from '@/store/mutation-types'

export default {
  namespaced: true,
  state: {
    goodsInfo: {} || []
  },
  actions: {
    async getGoodsInfo({ commit }, goodId) {
      const res = await reqGoodsInfo(goodId)
      if (res.code == 200) {
        commit(GOODS_INFO, res.data)
      } else {
        console.log('Goods组件仓库请求goodsInfo数据失败')
      }
    },
    async addUpdateCars(_, { goodsId, goodsNum }) {
      const res = await reqAddUpdateCar(goodsId, goodsNum)
      if (res.code == 200) {
        return Promise.resolve("添加成功")
      } else {
        return Promise.reject("添加失败")
      }
    }
  },
  mutations: {
    [GOODS_INFO]: (context, data) => context.goodsInfo = data
  },
  getters: {
    categoryView(state) {
      return state.goodsInfo.categoryView || {}
    },
    skuInfo(state) {
      return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
      return state.goodsInfo.spuSaleAttrList
    },
    valuesSkuJson(state) {
      return state.goodsInfo.spuSaleAttrList
    }
  }

}